package com.cade.agriculturemall.view.refresh;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


import com.cade.agriculturemall.utils.DisplayUtil;

import static android.view.animation.Animation.INFINITE;

/**
 * Created by tom on 17/7/12.
 */

public class RefreshAnimationView extends View{

    public RefreshAnimationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#2D96DE"));
        mPaint.setAntiAlias(true);
    }

    private Paint mPaint;

    private boolean animationning = false;
    private int animationIndex = 0;
    private ValueAnimator valueAnimator;

    private int normalWidth = DisplayUtil.dip2px(getContext(), 16);
    private int normalHeight = DisplayUtil.dip2px(getContext(), 24);
    private float radus = DisplayUtil.dip2px(getContext(), 3);

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (animationning){
            int height = normalHeight;
            if (animationIndex == 1) {
                height = (int) (normalHeight * 1.2f);
                mPaint.setColor(Color.argb(255, 45, 150, 222));
            }else {
                mPaint.setColor(Color.argb(120, 45, 150, 222));
            }
            float l = getWidth() / 2 - normalWidth / 2;
            float t = getHeight() / 2 - height / 2;
            RectF rectF = new RectF(l, t, l + normalWidth, t + height);
            canvas.drawRoundRect(rectF, radus, radus, mPaint);

            height = normalHeight;
            if (animationIndex == 0) {
                height = (int) (normalHeight * 1.2f);
                mPaint.setColor(Color.argb(255, 45, 150, 222));
            }else {
                mPaint.setColor(Color.argb(120, 45, 150, 222));
            }
            t = getHeight() / 2 - height / 2;
            l -= normalWidth * 1.5;
            rectF = new RectF(l, t, l + normalWidth, t + height);
            canvas.drawRoundRect(rectF, radus, radus, mPaint);

            height = normalHeight;
            if (animationIndex == 2) {
                height = (int) (normalHeight * 1.2f);
                mPaint.setColor(Color.argb(255, 45, 150, 222));
            }else {
                mPaint.setColor(Color.argb(120, 45, 150, 222));
            }
            t = getHeight() / 2 - height / 2;
            l += normalWidth * 3;
            rectF = new RectF(l, t, l + normalWidth, t + height);
            canvas.drawRoundRect(rectF, radus, radus, mPaint);
        }else{

            mPaint.setColor(Color.argb(mAlpha, 45, 150, 222));

            float l = getWidth() / 2 - normalWidth / 2;
            float t = getHeight() / 2 - normalHeight / 2;
            RectF rectF = new RectF(l, t, l + normalWidth, t + normalHeight);
            canvas.drawRoundRect(rectF, radus, radus, mPaint);

            l -= normalWidth * 1.5;
            rectF = new RectF(l, t, l + normalWidth, t + normalHeight);
            canvas.drawRoundRect(rectF, radus, radus, mPaint);

            l += normalWidth * 3;
            rectF = new RectF(l, t, l + normalWidth, t + normalHeight);
            canvas.drawRoundRect(rectF, radus, radus, mPaint);
        }
    }

    public void startAnimation(){
        valueAnimator = ValueAnimator.ofInt(0, 3);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animationIndex = (int) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                animationning = true;
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {
                animationning = false;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.setRepeatCount(INFINITE);
        valueAnimator.setDuration(300);
        valueAnimator.start();
    }

    public void cancel(){
        if (valueAnimator != null){
            valueAnimator.cancel();
        }
    }

    private int mAlpha = 0;

    public void setRectAlpha(float p){
        if (p >= 1){
            p = 1;
        }
        if (p <= 0.3f){
            p = 0.3f;
        }
        mAlpha = (int) (255 * p);
        postInvalidate();
    }
}
